package com.rabbitmq.rabbitmqspring.config;

import com.rabbitmq.rabbitmqspring.constant.Constants;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    /**
     * 工作队列模式
     * @return
     */
    @Bean("workQueue")
    public Queue workQueue() {
        return QueueBuilder.durable(Constants.WORK_QUEUE).build();
    }

    /**
     * 发布订阅模式
     * @return
     */
    @Bean("fanoutQueue1")
    public Queue fanoutQueue1() {
        return QueueBuilder.durable(Constants.FANOUT_QUEUE1).build();
    }

    @Bean("fanoutQueue2")
    public Queue fanoutQueue2() {
        return QueueBuilder.durable(Constants.FANOUT_QUEUE2).build();
    }

    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange() {
        return ExchangeBuilder.fanoutExchange(Constants.FANOUT_EXCHANGE).durable(true).build();
    }

    /**
     * 绑定交换机与队列
     */
    @Bean("fanoutBind1")
    public Binding fanoutBind1(@Qualifier("fanoutExchange") FanoutExchange exchange,
                               @Qualifier("fanoutQueue1") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    @Bean("fanoutBind2")
    public Binding fanoutBind2(@Qualifier("fanoutExchange") FanoutExchange exchange,
                               @Qualifier("fanoutQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    /**
     * 路由模式
     */
    @Bean("directQueue1")
    public Queue directQueue1() {
        return QueueBuilder.durable(Constants.DIRECT_QUEUE1).build();
    }

    @Bean("directQueue2")
    public Queue directQueue2() {
        return QueueBuilder.durable(Constants.DIRECT_QUEUE2).build();
    }

    @Bean("directExchange")
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange(Constants.DIRECT_EXCHANGE).durable(true).build();
    }

    /**
     * 绑定交换机与队列
     */
    @Bean("directBind1")
    public Binding directBind1(@Qualifier("directExchange") DirectExchange directExchange,
                               @Qualifier("directQueue1") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.DIRECT_ROUTINGKEY1);
    }
    @Bean("directBind2")
    public Binding directBind2(@Qualifier("directExchange") DirectExchange directExchange,
                               @Qualifier("directQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.DIRECT_ROUTINGKEY2);
    }
    @Bean("directBind3")
    public Binding directBind3(@Qualifier("directExchange") DirectExchange directExchange,
                               @Qualifier("directQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.DIRECT_ROUTINGKEY1);
    }

    /**
     * 通配符模式
     */
    @Bean("topicQueue1")
    public Queue topicQueue1() {
        return QueueBuilder.durable(Constants.TOPIC_QUEUE1).build();
    }
    @Bean("topicQueue2")
    public Queue topicQueue2() {
        return QueueBuilder.durable(Constants.TOPIC_QUEUE2).build();
    }
    @Bean("topicExchange")
    public TopicExchange topicExchange() {
        return ExchangeBuilder.topicExchange(Constants.TOPIC_EXCHANGE).durable(true).build();
    }

    /**
     * 绑定交换机与队列
     */
    @Bean("topicBind1")
    public Binding topicBind1(@Qualifier("topicExchange") TopicExchange topicExchange,
                              @Qualifier("topicQueue1") Queue queue) {
        return BindingBuilder.bind(queue).to(topicExchange).with(Constants.TOPIC_ROUTINGKEY1);
    }
    @Bean("topicBind2")
    public Binding topicBind2(@Qualifier("topicExchange") TopicExchange topicExchange,
                              @Qualifier("topicQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(topicExchange).with(Constants.TOPIC_ROUTINGKEY2);
    }
    @Bean("topicBind3")
    public Binding topicBind3(@Qualifier("topicExchange") TopicExchange topicExchange,
                              @Qualifier("topicQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(topicExchange).with(Constants.TOPIC_ROUTINGKEY3);
    }

}
